<?php 
/*
 module:		活动列表
 create_time:	2022-03-08 12:04:02
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use app\api\service\ZhActivityService;
use app\api\model\ZhActivity as ZhActivityModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use app\backend\model\SysSite as SysSiteModel;
use app\api\controller\Jwt;
use app\api\model\Member as MemberModel;
use app\backend\model\ZhSignUp as ZhSignUpModel;

class ZhActivity extends Common {



	/**
	* @api {get} /ZhActivity/index 01、首页数据列表
	* @apiGroup ZhActivity
	* @apiVersion 1.0.0
	* @apiDescription  首页数据列表
	* @apiParam (输入参数：) {int}     		[limit] 每页数据条数（默认20）
	* @apiParam (输入参数：) {int}     		[page] 当前页码
	* @apiParam (输入参数：) {string}		[title] 标题 
	* @apiParam (输入参数：) {string}		[subtitle] 简介 
	* @apiParam (输入参数：) {string}		[start_time_start] 活动开始时间开始
	* @apiParam (输入参数：) {string}		[start_time_end] 活动开始时间结束
	* @apiParam (输入参数：) {string}		[address] 地址 
	* @apiParam (输入参数：) {int}			[is_charge] 是否收费 是|1|success,否|0|warning

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据
	* @apiParam (成功返回参数：) {string}     	array.data.list 返回数据列表
	* @apiParam (成功返回参数：) {string}     	array.data.count 返回数据总数
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"查询失败"}
	*/
	function index(){
        $wxapp_id = $this->request->get('wxapp_id');
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
        $type  = $this->request->get('type');// 0|全部 1|进行中 2|已结束
        $title =  $this->request->get('title','','serach_in');
        // 获取用户id 用以判断用户是否登录
//        $token = $this->request->header('Authorization');
//        $uid   = 0;
//        if($token){
//            $jwt  = Jwt::getInstance();
//            $jwt->setIss(config('my.jwt_iss'))->setAud(config('my.jwt_aud'))
//                                                    ->setSecrect(config('my.jwt_secrect'))
//                                                    ->setToken($token);
//            $uid  = $jwt->decode()->getClaim('uid');
//            $user = MemberModel::where(['uid' => $uid, 'wxapp_id' => $wxapp_id])->find();
//            if(!$user){
//                return $this->ajaxReturn($this->errorCode,'未知用户');
//            }
//            $myActivityId = ZhSignUpModel::where([
//                                                'wxapp_id'   => $wxapp_id,
//                                                'u_id'       => $uid,
//                                                'status'     => 1
//                                                ])->column('activity_id');
//        }
        
        
		$limit           = $this->request->get('limit', 20, 'intval');
		$page            = $this->request->get('page', 1, 'intval');
		$where           = [];
        $where['status'] = 1;
        $where['wxapp_id'] = $wxapp_id;
        $field           = '*';
        $orderby         = 'activity_id desc';
        $query = ZhActivityModel::where($where)
                              ->field($field)
                              ->order($orderby);
        // 用户已登录
        $newTime = time();
        // 进行中
        if($type == 1){
            $query = $query->whereTime('sign_up_end_time','>',$newTime);
        }
        // 已结束
        if($type == 2){
            $query = $query->whereTime('sign_up_end_time','<',$newTime);
        }
        
        if($title){
            $query = $query->where('title','like','%' . $title . '%');
        }
        
        $res = $query->paginate(['list_rows'=>$limit,'page'=>$page])
                     ->toArray();
                     
        foreach ($res['data'] as &$v){
            if($newTime <= $v['sign_up_end_time']){
                $v['type'] = '进行中';
            }
//            if($v['sign_up_start_time'] <= $newTime && $newTime <= $v['sign_up_end_time']){
//                $v['type'] = '去报名';
//            }
//            if($uid && in_array($v['activity_id'],$myActivityId)){
//                $v['type'] = '已报名';
//            }
            
//            if($v['sign_up_start_time'] > $newTime){
//                $v['type'] = '未开始';
//            }
            if($v['sign_up_end_time'] < $newTime){
                $v['type'] = '已结束';
            }
            $v['start_time']         = date('Y-m-d',$v['start_time']);
            $v['sign_up_start_time'] = date('Y-m-d',$v['sign_up_start_time']);
            $v['sign_up_end_time']   = date('Y-m-d',$v['sign_up_end_time']);
            $v['createtime']         = date('Y-m-d',$v['createtime']);
            $v['images']             = json_decode(html_out($v['images']),true);
            $v['coordinate']         = json_decode(html_out($v['coordinate']),true);
//            $v['fengmiantu']         = $this->request->domain() . $v['images'][0]['url'];
            $v['fengmiantu']         = $v['images'][0]['url'];
        }
        unset($v);
		return $this->ajaxReturn($this->successCode,'返回成功',htmlOutList($res));
	}

	/**
	* @api {get} /ZhActivity/view 02、查看详情
	* @apiGroup ZhActivity
	* @apiVersion 1.0.0
	* @apiDescription  查看详情
	
	* @apiParam (输入参数：) {string}     		activity_id 主键ID

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据详情
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":"201","msg":"没有数据"}
	*/
	function view(){
        $wxapp_id = $this->request->get('wxapp_id');
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
		$data['activity_id'] = $this->request->get('activity_id','','serach_in');
        $data['wxapp_id'] = $wxapp_id;
		$field='*';
		$res  = checkData(ZhActivityModel::field($field)->where($data)->find());
        
        // 获取用户id 用以判断用户是否登录
        $token = $this->request->header('Authorization');
        $uid   = 0;
        if($token){
            $jwt  = Jwt::getInstance();
            $jwt->setIss(config('my.jwt_iss'))->setAud(config('my.jwt_aud'))
                ->setSecrect(config('my.jwt_secrect'))
                ->setToken($token);
            $uid  = $jwt->decode()->getClaim('uid');
            $user = MemberModel::where(['uid' => $uid, 'wxapp_id' => $wxapp_id])->find();
            if(!$user){
                return $this->ajaxReturn($this->errorCode,'未知用户');
            }
            $myActivityId = ZhSignUpModel::where([
                'wxapp_id'   => $wxapp_id,
                'u_id'       => $uid,
                'status'     => 1
            ])->column('activity_id');
        }
        $newTime = time();
        if($res['sign_up_start_time'] <= $newTime && $newTime <= $res['sign_up_end_time']){
            $res['type'] = '去报名';
        }
        if($uid && in_array($res['activity_id'],$myActivityId)){
            $res['type'] = '已报名';
        }
        
        if($res['sign_up_start_time'] > $newTime){
            $res['type'] = '未开始';
        }
        if($res['sign_up_end_time'] < $newTime){
            $res['type'] = '已结束';
        }
        $res['start_time']         = date('Y-m-d H:i:s',$res['start_time']);
        $res['sign_up_start_time'] = date('Y-m-d',$res['sign_up_start_time']);
        $res['sign_up_end_time']   = date('Y-m-d',$res['sign_up_end_time']);
        $res['createtime']         = date('Y-m-d H:i:s',$res['createtime']);
        $res['images']             = array_column(json_decode(html_out($res['images']),true),'url');
        $res['coordinate']         = json_decode($res['coordinate']);
//        foreach ($res['images'] as &$v){
//            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
//        }
//        unset($v);
        $signUpNum = ZhSignUpModel::where(['activity_id'=> $data['activity_id'],'wxapp_id'=> $wxapp_id,'status'=> 1])->count('sign_up_id');
        $res['sign_up_num'] = $signUpNum;
		return $this->ajaxReturn($this->successCode,'返回成功',$res);
	}
	
	



}

